package com.wn.ticket.web;

import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.common.UploadUtils;
import com.wn.ticket.entity.Comment;
import com.wn.ticket.service.CommentService;
import com.wn.ticket.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/front/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UploadUtils uploadUtils;

    @Value("${upfile.filePath}")
    private String FilePath;


    @RequestMapping("/add")
    public void add(@RequestBody Comment comment, HttpServletRequest request) {

        MultipartHttpServletRequest multipartRequest =
                WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
        List<MultipartFile> file = multipartRequest.getFiles("myfiles");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取系统时间
        String t = df.format(System.currentTimeMillis());
        comment.setTime(t);
        comment.setLikes(0);//默认点赞数
        comment.setImg_link1(uploadUtils.filesUpload((MultipartFile[]) file.toArray()));//
        commentService.add(comment);
    }


    @RequestMapping("/filesUpload")
    //requestParam要写才知道是前台的那个数组
    public String filesUpload(@RequestParam("myfiles") MultipartFile[] files, HttpServletRequest request) {
        return  uploadUtils.filesUpload(files);
    }


    @RequestMapping("/like")
    public void Like(@RequestBody Comment comment) {
        int i = comment.getLikes() + 1;
        comment.setLikes(i);
        commentService.update(comment);
    }

    @RequestMapping("/getCommentById")
    public ResponseEntity getCommentById(int id) {
        CommentVo commentVo = new  CommentVo();
        Comment item  =  commentService.getCommentById(id);
        //BeanUtils.copyProperties(commentVo,item);
        String files=item.getImg_link1();
        String[] FileNames=files.split(",");
        String FileBase= FilePath + "\\" + "upload/";
        List<String> FilesList = new ArrayList<String>();
        for(String file :FileNames)
        {
            FilesList.add(FileBase+file);
        }
        commentVo.setCommentFiles(FilesList);
        commentVo.setComment(item);
        return  new ResponseEntity("200","ok",commentVo);
    }

    @RequestMapping("/getByCondition")
    public ResponseEntity getByCondition(@RequestBody Comment comment) {
        List<Comment> c = commentService.getByCondition(comment);
        List<CommentVo> commentVoList = new ArrayList<CommentVo>();
        String FileBase= FilePath + "\\" + "upload/";
        //循环评论 获取file文件并分割添加相对路径
        for (Comment item:c) {
            CommentVo commentVo = new CommentVo();
            String files=item.getImg_link1();
            String[] FileNames=files.split(",");
            List<String> FilesList = new ArrayList<String>();
             for(String file :FileNames)
             {
                 FilesList.add(FileBase+file);
             }
            commentVo.setCommentFiles(FilesList);
            commentVo.setComment(item);
            commentVoList.add(commentVo);
        }
        return new ResponseEntity("200", "ok", commentVoList);
    }


}
